package com.felix.algorithms.easy;

/**
 * InvertBinaryTree
 *
 * @author xuejinhua
 * @date 2018/9/14 10:58
 */
public class InvertBinaryTree {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public static TreeNode solution(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode t1 = root.left;
        TreeNode t2 = root.right;
        root.left = solution(t2);
        root.right = solution(t1);
        return root;
    }

    public boolean isSearchTree(TreeNode root) {
        if (root == null) {
            return true;
        }
        if (root.left != null && root.left.val > root.val) {
            return false;
        }
        if (root.right != null && root.right.val < root.val) {
            return false;
        }
        if (!isSearchTree(root.left) || !isSearchTree(root.right)){
            return false;
        }
        return true;
    }
}
